package padrao;

import Banco.*;

import org.omg.CORBA.*;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;

@SuppressWarnings("unused")
public class ClienteCaixa {

	private static caixa_eletronico server;

	@SuppressWarnings("null")
	public static caixa_eletronico getServer() {
		if (ClienteCaixa.server == null) {
			String[] args = new String[2];
			args[0] = "-ORBInitialPort";
			args[1] = "2500";
			ORB orb = ORB.init(args, null);
			org.omg.CORBA.Object objRef;
			try {
				objRef = orb.resolve_initial_references("NameService");
				NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
				String name = "CaixaEletronico";
				try {
					ClienteCaixa.server = caixa_eletronicoHelper.narrow(ncRef.resolve_str(name));
					return ClienteCaixa.server;
				} catch (NotFound e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CannotProceed e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (InvalidName e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return ClienteCaixa.server;
		}
		
		return ClienteCaixa.server;

	}

	public static void doSaque(int conta, double valor) {
		try {
			getServer().saque(conta, valor);
		} catch (conta_inval e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (saldo_insuf e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Fez a pegada!");
	}
	public static void doDeposito(int conta, double valor) {
		
		try {
			getServer().deposito(conta, valor);
		} catch (conta_inval e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Fez a pegada!");
	}

	public static void main(String args[]) {
		try {
			// Obtem o numero da conta
			System.out.println("SAQUE - Fala o numero ae: ");
			java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			String conta = reader.readLine();

			System.out.println("SAQUE - Fala o valor:");
			reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			String valor = reader.readLine();
			
			ClienteCaixa.doSaque(Integer.parseInt(conta), Double.parseDouble(valor));
			
			System.out.println("DEPOSITO - Fala o numero ae: ");
			reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			conta = reader.readLine();

			System.out.println("DEPOSITO - Fala o valor:");
			reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
			valor = reader.readLine();
			
			ClienteCaixa.doDeposito(Integer.parseInt(conta), Double.parseDouble(valor));

		} catch (Exception e) {
			System.out.println("ERROR : " + e);
			e.printStackTrace(System.out);
		}
	}

}
